import axios from 'axios';
import type {
  ApiResponse,
  PageResult,
  Services,
  ServicesQuery,
  Medical,
  MedicalQuery,
  Appointments,
  AppointmentsQuery,
  Checkups,
  CheckupsQuery,
  Resources,
  ResourcesQuery,
  Staffs,
  StaffsQuery,
  Dewormings,
  DewormingQuery,
  Vaccine,
  VaccineQuery,
  Pets,
  PetsQuery,
  Users,
  UsersQuery,
  ServiceRecords,
  ServiceRecordsQuery,
  User,
  LoginRequest,
  LoginResponse,
  UserQuery,
  AppointmentChartData
} from '@/types';

// 创建axios实例
const api = axios.create({
  baseURL: '/api', // 根据实际后端地址配置
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    // 可以在这里添加token等认证信息
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    console.error('API请求错误:', error);
    return Promise.reject(error);
  }
);

// 服务项目API
export const servicesApi = {
  // 分页查询服务项目
  getPage: (params: ServicesQuery): Promise<ApiResponse<PageResult<Services>>> =>
    api.get('/services/page', { params }),

  // 添加服务项目
  add: (data: Services): Promise<ApiResponse<void>> =>
    api.post('/services/add', data),

  // 修改服务项目
  update: (data: Services): Promise<ApiResponse<void>> =>
    api.put('/services/update', data),

  // 删除服务项目
  delete: (serviceId: number): Promise<ApiResponse<void>> =>
    api.delete(`/services/delete/${serviceId}`),

  // 根据ID查询服务项目
  getById: (serviceId: number): Promise<ApiResponse<Services>> =>
    api.get(`/services/${serviceId}`)
};

// 医疗记录API
export const medicalApi = {
  // 添加医疗记录
  add: (data: Medical): Promise<ApiResponse<string>> =>
    api.post('/medical/add', data),

  // 根据ID查询医疗记录
  getById: (medicalId: number): Promise<ApiResponse<Medical>> =>
    api.get(`/medical/get/${medicalId}`),

  // 修改医疗记录
  update: (data: Medical): Promise<ApiResponse<string>> =>
    api.put('/medical/update', data),

  // 批量删除医疗记录（POST）
  delete: (ids: number[]): Promise<ApiResponse<string>> =>
    api.post('/medical/delete', ids),

  // 分页查询医疗记录列表
  getPage: (params: MedicalQuery): Promise<ApiResponse<PageResult<Medical>>> =>
    api.get('/medical/page', { params })
};

// 预约记录API
export const appointmentsApi = {
  // 添加预约记录
  add: (data: Appointments): Promise<ApiResponse<string>> =>
    api.post('/appointments/add', data),

  // 更新预约记录
  update: (appointmentId: number, data: Appointments): Promise<ApiResponse<string>> =>
    api.put(`/appointments/update/${appointmentId}`, data),

  // 删除预约记录（多条）
  delete: (appointmentIds: string): Promise<ApiResponse<string>> =>
    api.delete(`/appointments/delete/${appointmentIds}`),

  // 根据ID查询预约记录
  getById: (appointmentId: number): Promise<ApiResponse<Appointments>> =>
    api.get(`/appointments/get/${appointmentId}`),

  // 分页查询预约记录列表
  getPage: (params: AppointmentsQuery): Promise<ApiResponse<PageResult<Appointments>>> =>
    api.get('/appointments/page', { params }),

  // 查询所有预约记录
  getAll: (): Promise<ApiResponse<Appointments[]>> =>
    api.get('/appointments/list')
};

// 体检记录API
export const checkupsApi = {
  // 添加体检记录
  add: (data: Checkups): Promise<ApiResponse<string>> =>
    api.post('/checkups/add', data),

  // 更新体检记录
  update: (data: Checkups): Promise<ApiResponse<string>> =>
    api.put('/checkups/update', data),

  // 批量删除体检记录（POST）
  delete: (ids: number[]): Promise<ApiResponse<string>> =>
    api.post('/checkups/delete', ids),

  // 根据ID查询体检记录
  getById: (checkupId: number): Promise<ApiResponse<Checkups>> =>
    api.get(`/checkups/get/${checkupId}`),

  // 分页查询体检记录列表
  getPage: (params: CheckupsQuery): Promise<ApiResponse<PageResult<Checkups>>> =>
    api.get('/checkups/page', { params })
};

// 服务资源API
export const resourcesApi = {
  // 添加服务资源
  add: (data: Resources): Promise<ApiResponse<void>> =>
    api.post('/resources/add', data),

  // 删除服务资源
  delete: (resourceId: number): Promise<ApiResponse<void>> =>
    api.delete(`/resources/delete/${resourceId}`),

  // 修改服务资源
  update: (data: Resources): Promise<ApiResponse<void>> =>
    api.put('/resources/update', data),

  // 分页查询服务资源
  getPage: (params: ResourcesQuery): Promise<ApiResponse<PageResult<Resources>>> =>
    api.get('/resources/page', { params }),

  // 根据ID查询服务资源
  getById: (resourceId: number): Promise<ApiResponse<Resources>> =>
    api.get(`/resources/${resourceId}`)
};

// 服务人员API
export const staffsApi = {
  // 添加服务人员
  add: (data: Staffs): Promise<ApiResponse<void>> =>
    api.post('/staffs/add', data),

  // 删除服务人员
  delete: (staffId: number): Promise<ApiResponse<void>> =>
    api.delete(`/staffs/delete/${staffId}`),

  // 修改服务人员
  update: (data: Staffs): Promise<ApiResponse<void>> =>
    api.put('/staffs/update', data),

  // 分页查询服务人员
  getPage: (params: StaffsQuery): Promise<ApiResponse<PageResult<Staffs>>> =>
    api.get('/staffs/page', { params }),

  // 根据ID查询服务人员
  getById: (staffId: number): Promise<ApiResponse<Staffs>> =>
    api.get(`/staffs/${staffId}`)
};

// 驱虫记录API
export const dewormingsApi = {
  // 添加驱虫记录
  add: (data: Dewormings): Promise<ApiResponse<string>> =>
    api.post('/dewormings/add', data),

  // 根据ID查询驱虫记录
  getById: (dewormingId: number): Promise<ApiResponse<Dewormings>> =>
    api.get(`/dewormings/get/${dewormingId}`),

  // 更新驱虫记录
  update: (data: Dewormings): Promise<ApiResponse<string>> =>
    api.put('/dewormings/update', data),

  // 批量删除驱虫记录（POST）
  delete: (ids: number[]): Promise<ApiResponse<string>> =>
    api.post('/dewormings/delete', ids),

  // 分页查询驱虫记录列表
  getPage: (params: DewormingQuery): Promise<ApiResponse<PageResult<Dewormings>>> =>
    api.get('/dewormings/page', { params })
};

// 疫苗记录API
export const vaccineApi = {
  // 添加疫苗记录
  add: (data: Vaccine): Promise<ApiResponse<string>> =>
    api.post('/vaccine/add', data),

  // 根据ID查询疫苗记录
  getById: (vaccineId: number): Promise<ApiResponse<Vaccine>> =>
    api.get(`/vaccine/get/${vaccineId}`),

  // 更新疫苗记录
  update: (data: Vaccine): Promise<ApiResponse<string>> =>
    api.put('/vaccine/update', data),

  // 批量删除疫苗记录（POST）
  delete: (ids: number[]): Promise<ApiResponse<string>> =>
    api.post('/vaccine/delete', ids),

  // 分页查询疫苗记录列表
  getPage: (params: VaccineQuery): Promise<ApiResponse<PageResult<Vaccine>>> =>
    api.get('/vaccine/page', { params })
};

// 宠物信息API
export const petsApi = {
  // 添加宠物
  add: (data: Pets): Promise<ApiResponse<void>> =>
    api.post('/pets/add', data),

  // 删除宠物
  delete: (petId: number): Promise<ApiResponse<void>> =>
    api.delete(`/pets/delete/${petId}`),

  // 批量删除宠物
  batchDelete: (ids: number[]): Promise<ApiResponse<void>> =>
    api.delete('/pets/delete', { data: ids }),

  // 修改宠物
  update: (data: Pets): Promise<ApiResponse<void>> =>
    api.put('/pets/update', data),

  // 分页查询宠物
  getPage: (params: PetsQuery): Promise<ApiResponse<PageResult<Pets>>> =>
    api.get('/pets/page', { params }),

  // 根据宠物ID查询宠物
  getById: (petId: number): Promise<ApiResponse<Pets>> =>
    api.get(`/pets/${petId}`),

  // 根据宠物名字模糊查询宠物
  getByName: (petName: string): Promise<ApiResponse<Pets>> =>
    api.get(`/pets/${petName}`)
};

// 客户档案API
export const usersApi = {
  // 分页查询客户
  getPage: (params: UsersQuery): Promise<ApiResponse<PageResult<Users>>> =>
    api.get('/users/page', { params }),

  // 添加客户
  add: (data: Users): Promise<ApiResponse<void>> =>
    api.post('/users/add', data),

  // 修改客户
  update: (data: Users): Promise<ApiResponse<void>> =>
    api.put('/users/update', data),

  // 删除客户
  delete: (userId: number): Promise<ApiResponse<void>> =>
    api.delete(`/users/delete/${userId}`),
  
  // 批量删除客户
  batchDelete: (ids: number[]): Promise<ApiResponse<void>> =>
    api.delete('/users/delete', { data: ids }),

  // 根据ID查询客户
  getById: (userId: number): Promise<ApiResponse<Users>> =>
    api.get(`/users/${userId}`)
};

// 服务执行记录API
export const serviceRecordsApi = {
  // 分页查询服务执行记录
  getPage: (params: ServiceRecordsQuery): Promise<ApiResponse<PageResult<ServiceRecords>>> =>
    api.get('/service-records/page', { params }),

  // 添加服务执行记录
  add: (data: ServiceRecords): Promise<ApiResponse<void>> =>
    api.post('/service-records/add', data),

  // 修改服务执行记录
  update: (data: ServiceRecords): Promise<ApiResponse<void>> =>
    api.put('/service-records/update', data),

  // 删除服务执行记录
  delete: (recordId: number): Promise<ApiResponse<void>> =>
    api.delete(`/service-records/delete/${recordId}`),

  // 根据ID查询服务执行记录
  getById: (recordId: number): Promise<ApiResponse<ServiceRecords>> =>
    api.get(`/service-records/${recordId}`)
};

// 用户认证API
export const authApi = {
  // 用户登录
  login: (data: LoginRequest): Promise<ApiResponse<any>> =>
    api.post('/user/login', data),

  // 获取当前用户信息
  getCurrentUser: (): Promise<ApiResponse<User>> =>
    api.get('/user/current'),

  // 用户登出
  logout: (): Promise<ApiResponse<void>> =>
    api.post('/logout')
};

// 用户管理API
export const userApi = {
  // 分页查询用户
  getPage: (params: UserQuery): Promise<ApiResponse<PageResult<User>>> =>
    api.get('/users/page', { params }),

  // 添加用户
  add: (data: User): Promise<ApiResponse<void>> =>
    api.post('/users/add', data),

  // 修改用户
  update: (data: User): Promise<ApiResponse<void>> =>
    api.put('/users/update', data),

  // 删除用户
  delete: (userId: number): Promise<ApiResponse<void>> =>
    api.delete(`/users/delete/${userId}`),

  // 根据ID查询用户
  getById: (userId: number): Promise<ApiResponse<User>> =>
    api.get(`/users/${userId}`)
};

// 预约图表数据API
export const chartApi = {
  // 获取预约图表数据
  getAppointmentChartData: (range: 'week' | 'month'): Promise<ApiResponse<AppointmentChartData[]>> =>
    api.get(`/chart/getAppointmentData`, { params: { range } })
};

export default api; 